<template>
<!--
完善个人信息
-->
<div style="background: darkgray;width: 100%;height: 950px">
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <div style="margin-left: 25%">
      <el-upload
        class="avatar-uploader"
        :show-file-list="false"
        action="https://jsonplaceholder.typicode.com/posts/"
        :before-upload="beforeAvatarUpload"
        :http-request="uploadFile"
        >
        <img v-if="this.ruleForm.imageUrl" :src="this.file2" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <el-form-item label="姓名：" prop="name">
      <el-input v-model="ruleForm.name" style="width: 46%"></el-input>
    </el-form-item>
      <el-form-item label="所在项目组" prop="region">
        <el-select v-model="ruleForm.region" placeholder="请选择小组">
          <el-option label="前端开发" value="前端开发"></el-option>
          <el-option label="后端开发" value="后端开发"></el-option>
          <el-option label="运维组" value="运维组"></el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="联系电话" prop="call1">-->
<!--        <el-input v-model="ruleForm.call1" style="width: 46%"></el-input>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </div>

  </el-form>
<!--  <div style="width: 60%;height: 600px;background: #B3C0D1;position:relative;left: 20%;top: 10%"></div>-->
</div>
</template>

<script>
import axios from "axios";

export default {
  name: "PerfectInfor",
  data(){
    return{
      file1:"",
      file2:"",
      imgName:"",
      ruleForm: {
        name: '',
        region: '',
      },
      rules: {
        name: [
          { required: true, message: '请输入你的姓名', trigger: 'blur' },
          { min: 2, max: 3, message: '长度在 2 到 3 个字符', trigger: 'blur' }
        ],
        region: [
          { required: true, message: '请选择你的项目组', trigger: 'change' }
        ],
      }
    }
  },
  methods: {
    submitForm(formName) {

      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios({
            url:'/infor/perfect',
            method: 'post',
            data:{
              imgFile:this.file2,
              ruleForm:this.ruleForm,
              name:this.ruleForm.name,
              region:this.ruleForm.region,
              imgName:this.imgName,
            }
          }).then(res =>{
            if (res.data){
              this.$message({
                message:"提交成功",
                type:'success'
              })
            }else {
              this.$message({
                message:"提交失败",
                type:'error'
              })
            }
          })
          alert('submit!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    //上传头像
    uploadFile(item) {
      let fileObj = item.file;
      this.file1 = fileObj
      this.imgName = this.file1.name
      console.log(fileObj )
      if (window.FileReader){
        let fr = new FileReader();
        fr.onloadend = (e) =>{
         this.ruleForm.imageUrl =  e.target.result
          this.file2 =e.target.result

        }
        fr.readAsDataURL(fileObj)

      }

    },

    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }

      return isJPG && isLt2M;
    }
  }
}
</script>

<style scoped>
.demo-ruleForm{
  background: white;
  width: 40%;
  height: 600px;
  position: relative;
  left: 30%;
  top: 10%;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

/*上传头像样式*/
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
